package com.youan.log.modules.audit.controller;

import com.youan.log.modules.audit.dataimport.bean.TaskData;
import com.youan.log.modules.audit.dto.ImportRecordDTO;
import com.youan.log.modules.audit.entity.ImportRecord;
import com.youan.log.modules.audit.service.IImportRecordService;
import com.youan.log.modules.audit.service.IImportService;
import com.youan.log.modules.audit.wrapper.ImportRecordWrapper;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/import")
public class ImportController {
    @Autowired
    private IImportService importService;
    @Autowired
    private IImportRecordService importRecordService;

    @GetMapping("/getDataHasImportedTaskData")
    public R<List<TaskData>> getDataHasImportedTaskData() {
        List<TaskData> result = importService.getDataHasImportedTaskData();
        return R.data(result);
    }

    @GetMapping("/getCanImportTaskData")
    public R<List<TaskData>> getCanImportTaskData() {
        List<TaskData> result = importService.getCanImportTaskData();
        return R.data(result);
    }

    @GetMapping("/getRunningTaskStatus")
    public R<List<ImportRecordDTO>> getRunningTaskStatus() {
        List<ImportRecord> runningRecords = importRecordService.getAllRunningRecords();
        List<ImportRecordDTO> result = runningRecords.stream().map(ImportRecordWrapper::build).collect(Collectors.toList());
        return R.data(result);
    }

    @PostMapping("/startImport")
    public void startImport(@RequestBody List<String> importDates) {
        importService.startImport(importDates);
    }

    @PostMapping("/deleteTaskDataFile")
    public R deleteTaskDataFile(String date) {
        importService.deleteTaskDataFile(date);
        return R.status(true);
    }

}
